home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / CDTools / MUIRexx / demos / MUIRexxDir / comm.rexx < prev    next >
OS/2 REXX Batch file  |  1997-03-28  |  16KB  |  581 lines

  1. /*
  2.  
  3. Code:       comm.rexx
  4. Author:     Russell Leighton
  5. Revision:   11 Jan 1996
  6.  
  7. Comments:   This is script used to perform file operations.  It is executed
  8. when certain gadgets are selected in the muidir GUI.
  9.  
  10. */
  11.  
  12. options results
  13. signal on error
  14.  
  15. /* TAG ID definitions */
  16.  
  17. Draggable =                       0x80420b6e /* V11 isg BOOL              */
  18. Dirlist_Directory =               0x8042ea41 /* V4  isg STRPTR            */
  19. Dirlist_RejectIcons =             0x80424808 /* V4  is. BOOL              */
  20. List_Quiet =                      0x8042d8c7 /* V4  .s. BOOL              */
  21.  
  22. /* TAG variable definitions */
  23.  
  24. TRUE = 1
  25. FALSE = 0
  26. List_Insert_Bottom = -3
  27.  
  28. parse arg portname comm' ['name']'
  29.  
  30. call pragma('Directory','muidir:')
  31.  
  32. address VALUE portname
  33.  
  34. comm = strip(comm)
  35.  
  36. group ID DIR REGISTER
  37. ndir = result
  38.  
  39. select
  40.  
  41. /* copy the selected files from the source to destination directory */
  42.  
  43.     when index(comm,'COPY') = 1 then call copyfiles(3)
  44.  
  45. /* move the selected files from the source to destination directory */
  46.  
  47.     when index(comm,'MOVE') = 1 then do
  48.         dirlist ID DIR||ndir ATTRS Dirlist_Directory
  49.         sdir = import(d2c(result))
  50.         dirlist ID DIR||(3-ndir) ATTRS Dirlist_Directory
  51.         ddir = import(d2c(result))
  52.         if substr(sdir,1,pos(':',sdir)) = substr(ddir,1,pos(':',ddir)) then
  53.             copy = 1
  54.         else copy = 2
  55.         call copyfiles(copy)
  56.     end
  57.  
  58. /* delete the selected files from the source directory */
  59.  
  60.     when index(comm,'DELETE') = 1 then do
  61.         request ID MDIR TITLE '" "' GADGETS '"OK|Cancel"' "Delete selected entries?"
  62.         if result = 1 then do
  63.             do forever
  64.                 dirlist ID DIR||ndir
  65.                 sfile = result
  66.                 if sfile = '' then break
  67.                 address command 'delete > T:err "'sfile'" ALL QUIET'
  68.                 list ID HST INSERT POS List_Insert_Bottom STRING 'delete 'sfile' ALL QUIET'
  69.                 check ID ICN||ndir
  70.                 if result = '1' & exists(sfile'.info') then do
  71.                     address command 'delete > T:err "'sfile'.info" ALL QUIET'
  72.                     list ID HST INSERT POS List_Insert_Bottom STRING 'delete 'sfile'.info ALL QUIET'
  73.                 end
  74.             end
  75.             if exists('T:err') then address command 'delete T:err quiet'
  76.             check ID ICN||ndir
  77.             dirlist ID DIR||ndir REREAD ATTRS Dirlist_RejectIcons result
  78.         end
  79.     end
  80.  
  81. /* either display the selected files icon or change source to the selected 
  82.    directory */
  83.  
  84.     when index(comm,'DIR') = 1 then do
  85.         name = strip(name)
  86.         if index(statef(name),'DIR') ~= 0 then do
  87.  
  88. /* the selected entry is a directory, so change to it */
  89.  
  90.             call changedir(name)
  91.             exit
  92.         end
  93.         else do
  94.  
  95. /* otherwise, if the entry does not exist assume it is suppose to be a
  96. directory and create it */
  97.  
  98.             if ~exists(name) then do
  99.                 address command 'makedir "'name'"'
  100.                 list ID HST INSERT POS List_Insert_Bottom STRING 'makedir 'name
  101.                 if rc = 0 then call changedir(name)
  102.                 exit
  103.             end
  104.             call defcomm portname name
  105.         end
  106.     end
  107.     when index(comm,'ICON') = 1 then do
  108.         name = strip(name)
  109.         if lastpos('/',name) ~= 0 then file = substr(name,lastpos('/',name)+1)
  110.         else file = substr(name,lastpos(':',name)+1)
  111.  
  112. /*      text ID NAM||ndir LABEL file */
  113.  
  114.         group ID GRP||ndir
  115.             button ID IMG||ndir ICON deficon(name) COMMAND '"muidir:defcomm 'portname' %s"' ATTRS Draggable TRUE LABEL name
  116.         endgroup
  117.     end
  118.  
  119. /* clear the text icon and string */
  120.  
  121.     when index(comm,'CLEAR') = 1 then do
  122.         group ID GRP||ndir
  123.             image ID IMG||ndir ICON '""' LABEL ' '
  124.         endgroup
  125.     end
  126.     when index(comm,'HCLEAR') = 1 then do
  127.         list ID HST STRING
  128.     end
  129.     when index(comm,'HEXE') = 1 then do
  130.         do forever
  131.  
  132.         /* get next entry */
  133.  
  134.             list ID HST
  135.             line = result
  136.             if line = '' then break
  137.  
  138.         /* execute entry */
  139.  
  140.             address command line' >T:err'
  141.         end
  142.         if exists('T:err') then address command 'delete T:err quiet'
  143.     end
  144.  
  145. /* compile a list of files to copy from the source to destination directory */
  146.  
  147.     when index(comm,'CCOPY') = 1 then do
  148.  
  149.         /* clear the copy list */
  150.  
  151.         list ID CLST STRING
  152.  
  153.         /* get the source directory name */
  154.  
  155.         dirlist ID DIR||ndir ATTRS Dirlist_Directory
  156.         sname = import(d2c(result))
  157.  
  158.         /* get the destination directory name */
  159.  
  160.         dirlist ID DIR||(3-ndir) ATTRS Dirlist_Directory
  161.         dname = import(d2c(result))
  162.  
  163.         /* temporarily disable list update */
  164.  
  165.         list ID CLST ATTRS List_Quiet TRUE
  166.  
  167.         /* call the nextcopy procedure to process the current directory */
  168.  
  169.         call nextcopy sname';'dname
  170.  
  171.         /* update the copy list (i.e. display the contents) */
  172.  
  173.         list ID CLST ATTRS List_Quiet FALSE
  174.     end
  175.  
  176. /* perform copy based on list of compiled files */
  177.  
  178.     when index(comm,'MCOPY') = 1 then do
  179.         do forever
  180.  
  181.         /* get next entry */
  182.  
  183.             list ID CLST
  184.             line = result
  185.             if line = '' then break
  186.  
  187.         /* parse entry */
  188.  
  189.             parse var line sfile' -> 'dfile' ['flags']'
  190.  
  191.         /* execute copy */
  192.  
  193.             address command 'copy > T:err "'sfile'" to "'dfile'" 'flags' CLONE QUIET'
  194.         end
  195.         if exists('T:err') then address command 'delete T:err quiet'
  196.  
  197.         /* update dirlist */
  198.  
  199.         check ID ICN||(3-ndir)
  200.         dirlist ID DIR||(3-ndir) REREAD ATTRS Dirlist_RejectIcons result
  201.     end
  202.  
  203. /* compile a list of files to delete from the destination directory */
  204.  
  205.     when index(comm,'CDEL') = 1 then do
  206.  
  207.         /* clear the delete list */
  208.  
  209.         list ID DLST STRING
  210.  
  211.         /* get the source directory name */
  212.  
  213.         dirlist ID DIR||ndir ATTRS Dirlist_Directory
  214.         sname = import(d2c(result))
  215.  
  216.         /* get the destination directory name */
  217.  
  218.         dirlist ID DIR||(3-ndir) ATTRS Dirlist_Directory
  219.         dname = import(d2c(result))
  220.  
  221.         /* temporarily disable list update */
  222.  
  223.         list ID DLST ATTRS List_Quiet TRUE
  224.  
  225.         /* call the nextdel procedure to process the current directory */
  226.  
  227.         call nextdel sname';'dname
  228.  
  229.         /* update the delete list (i.e. display the contents) */
  230.  
  231.         list ID DLST ATTRS List_Quiet FALSE
  232.     end
  233.  
  234. /* perform delete based on list of compiled files */
  235.  
  236.     when index(comm,'MDEL') = 1 then do
  237.         do forever
  238.  
  239.         /* get next entry */
  240.  
  241.             list ID DLST
  242.             line = result
  243.             if line = '' then break
  244.  
  245.         /* parse entry */
  246.  
  247.             parse var line sfile' ['flags']'
  248.  
  249.         /* execute delete */
  250.  
  251.             address command 'delete > T:err "'sfile'" 'flags' QUIET'
  252.         end
  253.         if exists('T:err') then address command 'delete T:err quiet'
  254.  
  255.         /* update dirlist */
  256.  
  257.         check ID ICN||(3-ndir)
  258.         dirlist ID DIR||(3-ndir) REREAD ATTRS Dirlist_RejectIcons result
  259.     end
  260.  
  261. /* Edit selected files */
  262.  
  263.     when index(comm,'EDIT') = 1 then do
  264.         getvar screen
  265.         pub = result
  266.         files = ''
  267.         do forever
  268.             dirlist ID DIR||ndir
  269.             sfile = result
  270.             if sfile = '' then break
  271.             files = files' "'sfile'"'
  272.         end
  273.         address command 'ced -pubscreen='pub' 'files
  274.     end
  275.  
  276. /* View selected files */
  277.  
  278.     when index(comm,'VIEW') = 1 then do
  279.         files = ''
  280.         do forever
  281.             dirlist ID DIR||ndir
  282.             sfile = result
  283.             if sfile = '' then break
  284.             files = files' "'sfile'"'
  285.         end
  286.         address command 'tools:graphics/PicassoII/Viewer/IntuiView now'files
  287.     end
  288.  
  289. /* Make icons for selected files */
  290.  
  291.     when index(comm,'ICON') = 1 then do
  292.         files = ''
  293.         do forever
  294.             dirlist ID DIR||ndir
  295.             sfile = result
  296.             if sfile = '' then break
  297.             files = files' "'sfile'"'
  298.         end
  299.         address command 'tools:graphics/Picticon/Picticon ADDICON=YES CHUNKYMODE=YES FREE_ICON_POS=YES NEWICON=YES OVERWRITE=YES QUIET=YES TEMPLATE_ICON=tools:graphics/Picticon/envsys/def_picture'files
  300.     end
  301.  
  302. /* Execute selected files */
  303.  
  304.     when index(comm,'EXECUTE') = 1 then do
  305.         do forever
  306.             dirlist ID DIR||ndir
  307.             sfile = result
  308.             if sfile = '' then break
  309.             address command sfile
  310.         end
  311.     end
  312. end
  313.  
  314. exit
  315.  
  316. /* procedure to change the source dirlist directory */
  317.  
  318. changedir:
  319. parse arg name
  320.  
  321.     dirlist ID DIR||ndir ATTRS Dirlist_Directory
  322.     if result = 0 then dirname = ''
  323.     else dirname = import(d2c(result))
  324.  
  325. /* if the name is empty then set directory to RAM: */
  326.  
  327.     if strip(name) = '' then name = 'RAM:'
  328.  
  329. /* if the name is the currently displayed directory then just reread it */
  330.  
  331. /*
  332.     if name = dirname then do
  333.         check ID ICN||ndir
  334.         dirlist ID DIR||ndir REREAD ATTRS Dirlist_RejectIcons result
  335.         return
  336.     end
  337. */
  338.  
  339. /* if the name is a / then get parent directory */
  340.  
  341.     if index(name,'/') = 1 then do
  342.         if lastpos('/',dirname) ~= 0 then name = substr(dirname,1,lastpos('/',dirname)-1)
  343.         else name = substr(dirname,1,lastpos(':',dirname))
  344.     end
  345.  
  346. /* if the name is not a directory then extract directory name */
  347.  
  348.     if index(statef(name),'DIR') = 0 then do
  349.         if lastpos('/',name) ~= 0 then name = substr(name,1,lastpos('/',name)-1)
  350.         else name = substr(name,1,lastpos(':',name))
  351.     end
  352.  
  353. /* determine volume name and percentage full */
  354.  
  355.     vol = substr(name,1,pos(':',name))
  356.     address command 'assign >pipe: exists "'vol'"'
  357.     call open('pipe','pipe:','R')
  358.     line = readln('pipe')
  359.     call close('pipe')
  360.     parse var line vname aname
  361.     aname = strip(aname)
  362.     if index(aname,'[Mounted]') = 1 then vname = vname':'
  363.     else do
  364.         if aname = '' then vname = vol
  365.         else vname = substr(aname,1,lastpos(':',aname))
  366.     end
  367.     address command 'info >pipe: 'vname
  368.     call open('pipe','pipe:','R')
  369.     line = readln('pipe')
  370.     line = readln('pipe')
  371.     line = readln('pipe')
  372.     line = readln('pipe')
  373.     parse var line unit size used free full errs status vname
  374.     do until eof('pipe')
  375.         line = readln('pipe')
  376.     end
  377.     call close('pipe')
  378.  
  379.     if lastpos('/',name) = length(name) then name = substr(name,1,length(name)-1)
  380.  
  381. /* set appropriate gadgets with information */
  382.  
  383.     text ID TXT||ndir LABEL strip(vname)'  'full' full'
  384.     string ID SRC||ndir CONTENT name
  385.     check ID ICN||ndir
  386.     dirlist ID DIR||ndir PATH '"'name'"' ATTRS Dirlist_RejectIcons result
  387.     group ID REG REGISTER LABEL "Directory"
  388.     list ID LST NODUP INSERT STRING name
  389. return
  390.  
  391. /* procedure to copy, rename, or move files from source to destination */
  392.  
  393. copyfiles:
  394. parse arg type
  395.  
  396. dirlist ID DIR||(3-ndir) ATTRS Dirlist_Directory
  397. ddir = import(d2c(result))
  398. if lastpos('/',ddir) ~= length(ddir) then do
  399.     if lastpos(':',ddir) ~= length(ddir) then ddir = ddir'/'
  400. end
  401.  
  402. do forever
  403.  
  404. /* find next selected file */
  405.  
  406.     dirlist ID DIR||ndir
  407.     sfile = result
  408.  
  409. /* if there is no selected file then reread dirlist and return */
  410.  
  411.     if sfile = '' then do
  412.         check ID ICN||ndir
  413.         flag = result
  414.         dirlist ID DIR||ndir REREAD ATTRS Dirlist_RejectIcons flag
  415.         dirlist ID DIR||(3-ndir) REREAD ATTRS Dirlist_RejectIcons flag
  416.         return
  417.     end
  418.     if lastpos('/',sfile) ~= 0 then dfile = ddir||substr(sfile,lastpos('/',sfile)+1)
  419.     else dfile = ddir||substr(sfile,lastpos(':',sfile)+1)
  420.  
  421. /* if the file exists at the destination then put up a requester to allow
  422. the user to decide on the action */
  423.  
  424.     if exists(dfile) & (type > 0) then do
  425.         request ID MDIR TITLE '" "' GADGETS '"_OK|_ALL|_SKIP|_CANCEL"' '\033bFile already exists!\033n Do you wish to overwrite?'
  426.         select
  427.             when result = 0 then return
  428.             when result = 1 then nop
  429.             when result = 2 then type = 0-type
  430.             when result = 3 then iterate
  431.             otherwise return
  432.         end
  433.     end
  434.  
  435.     if type < 0 then ctype = 0 - type
  436.     else ctype = type
  437.  
  438.     call file portname ndir ctype sfile';'dfile
  439. end
  440. return
  441.  
  442. /* this procedure is used to determine names of files and directories to be
  443.    copied.  It is called recursivily to traverse directory trees. */
  444.  
  445. nextcopy: procedure
  446. parse arg sname';'dname
  447.  
  448. sname = strip(sname)
  449. dname = strip(dname)
  450. if sname = '' | dname = '' then return
  451.  
  452. /* get list of entries in source directory */
  453.  
  454. slist = showdir(sname,'ALL',';')
  455.  
  456. if lastpos('/',sname) ~= length(sname) then do
  457.     if lastpos(':',sname) ~= length(sname) then sname = sname'/'
  458. end
  459. if lastpos('/',dname) ~= length(dname) then do
  460.     if lastpos(':',dname) ~= length(dname) then dname = dname'/'
  461. end
  462.  
  463. /* process each entry */
  464.  
  465. do while slist ~= ''
  466.     parse var slist sfile';'slist
  467.  
  468. /* assemble full path names for source and destination files */
  469.  
  470.     dfile = dname||sfile
  471.     sfile = sname||sfile
  472.  
  473. /* get file information */
  474.  
  475.     state = statef(sfile)
  476.     parse var state type size blocks protect s1 s2 s3 .
  477.  
  478. /* create time numerical stamp */
  479.  
  480.     sstamp = right(s1,4,'0')||right(s2,4,'0')||right(s3,4,'0')
  481.  
  482. /* if destination file exists then do the following */
  483.  
  484.     if exists(dfile) then do
  485.  
  486. /* if entry is a directory then recursivily call this routine with the
  487.    subdirectory name as the argument */
  488.  
  489.         if index(type,'DIR') then call nextcopy sfile';'dfile
  490.  
  491. /* else compare the destination time stamp to the source */
  492.  
  493.         else do
  494.             state = statef(dfile)
  495.             parse var state type size blocks protect s1 s2 s3 .
  496.             dstamp = right(s1,4,'0')||right(s2,4,'0')||right(s3,4,'0')
  497.  
  498. /* if the source file is newer then add entry to copy list */
  499.  
  500.             if sstamp > dstamp then do
  501.                 list ID CLST INSERT POS List_Insert_Bottom STRING sfile '->' dfile' [] *'
  502.             end
  503.         end
  504.     end
  505.  
  506. /* if destination file does not exist then add entry to copy list */
  507.  
  508.     else do
  509.         if index(type,'DIR') then list ID CLST INSERT POS List_Insert_Bottom STRING sfile '->' dfile' [ALL]'
  510.         else list ID CLST INSERT POS List_Insert_Bottom STRING sfile '->' dfile' []'
  511.     end
  512. end
  513. return
  514.  
  515. /* this procedure is used to determine names of files and directories to be
  516.    deleted.  It is called recursivily to traverse directory trees. */
  517.  
  518. nextdel: procedure
  519. parse arg sname';'dname
  520.  
  521. sname = strip(sname)
  522. dname = strip(dname)
  523. if sname = '' | dname = '' then return
  524.  
  525. /* get list of entries in destination directory */
  526.  
  527. dlist = showdir(dname,'ALL',';')
  528.  
  529. if lastpos('/',sname) ~= length(sname) then do
  530.     if lastpos(':',sname) ~= length(sname) then sname = sname'/'
  531. end
  532. if lastpos('/',dname) ~= length(dname) then do
  533.     if lastpos(':',dname) ~= length(dname) then dname = dname'/'
  534. end
  535.  
  536. /* process each entry */
  537.  
  538. do while dlist ~= ''
  539.     parse var dlist dfile';'dlist
  540.  
  541. /* assemble full path names for source and destination files */
  542.  
  543.     sfile = sname||dfile
  544.     dfile = dname||dfile
  545.  
  546. /* get file information */
  547.  
  548.     state = statef(dfile)
  549.     parse var state type size blocks protect stamp
  550.  
  551. /* if source file exists then do the following */
  552.  
  553.     if exists(sfile) then do
  554.  
  555. /* if entry is a directory then recursivily call this routine with the
  556.  
  557.    subdirectory name as the argument */
  558.         if index(type,'DIR') then call nextdel sfile';'dfile
  559.     end
  560.  
  561. /* else add the file or directory to the delete list */
  562.  
  563.     else do
  564.         if index(type,'DIR') then list ID DLST INSERT POS List_Insert_Bottom STRING dfile' [ALL]'
  565.         else list ID DLST INSERT POS List_Insert_Bottom STRING dfile' []'
  566.     end
  567. end
  568. return
  569.  
  570. /* if an error occurs then display it */
  571.  
  572. error:
  573.     if exists('T:err') then do
  574.         call open('err','T:err')
  575.         err = readln('err')
  576.         call close('err')
  577.         address command 'delete T:err quiet'
  578.         request ID MDIR TITLE '" "' GADGETS '"OK"' err
  579.     end
  580. exit
  581.